import cv2

# 打开本地 Webcam
cap = cv2.VideoCapture(0)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

# 设置编码器
fourcc = cv2.VideoWriter_fourcc(*"mp4v")

# 设置 RTMP 服务器地址
rtmp_url = "rtmp://192.168.1.107:1935/live"
streamSize = (width,height)
streamOut = cv2.VideoWriter(rtmp_url, fourcc, 20.0, streamSize, True)
streamOpenFlag = False

# 持续捕获并保存视频帧
while cap.isOpened():
    ret, frame = cap.read()
    if ret:
        # 显示帧
        cv2.imshow('frame', frame)

        # 写入帧到 RTMP 服务器
        streamFrame = frame.resize(streamSize)
        streamOut.write(streamFrame)

        # 按 'q' 键退出循环
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

# 释放资源
streamOut.release()
cap.release()
cv2.destroyAllWindows()


